From 1b6a85d106374922bac8cce271b751ec3f5b284a Mon Sep 17 00:00:00 2001 From: robertl Date: Fri, 16 Aug 2002 22:46:41 +0000 Subject: [PATCH] gpspilot: new. gpspilot, cetus: Reject files that don't seem to be ours. --- cetus.c | 28 +++++--- gpspilot.c | 202 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 220 insertions(+), 10 deletions(-) create mode 100644 gpspilot.c diff --git a/cetus.c b/cetus.c index 01d943a94..9ed9fd933 100644 --- a/cetus.c +++ b/cetus.c @@ -23,6 +23,10 @@ #include "coldsync/palm.h" #include "coldsync/pdb.h" +#define MYNAME "Cetus" +#define MYTYPE 0x43577074 /* CWpt */ +#define MYCREATOR 0x63475053 /* cGPS */ + struct record { char type; char ID[16]; /* Zero-terminated string. */ @@ -60,7 +64,7 @@ rd_init(const char *fname) { file_in = fopen(fname, "r"); if (file_in == NULL) { - fatal("CETUS: Cannot open %s for reading\n", fname); + fatal(MYNAME ": Cannot open %s for reading\n", fname); } } @@ -76,7 +80,7 @@ wr_init(const char *fname) file_out = fopen(fname, "w"); out_fname = fname; if (file_out == NULL) { - fatal("CETUS: Cannot open %s for writing\n", fname); + fatal(MYNAME ": Cannot open %s for writing\n", fname); } } @@ -94,7 +98,11 @@ data_read(void) struct pdb_record *pdb_rec; if (NULL == (pdb = pdb_Read(fileno(file_in)))) { - fatal("pdb_Read failed"); + fatal(MYNAME ": pdb_Read failed"); + } + + if ((pdb->creator != MYCREATOR) || (pdb->type != MYTYPE)) { + fatal(MYNAME ": Not a Cetus file."); } for(pdb_rec = pdb->rec_index.rec; pdb_rec; pdb_rec=pdb_rec->next) { @@ -102,7 +110,7 @@ data_read(void) wpt_tmp = calloc(sizeof(*wpt_tmp),1); if (wpt_tmp == NULL) { - fatal("CETUS: cannot allocate memory\n"); + fatal(MYNAME ": cannot allocate memory\n"); } rec = (struct record *) pdb_rec->data; @@ -170,11 +178,11 @@ cetus_writewpt(waypoint *wpt) opdb_rec = new_Record (0, 0, ct++, sizeof(*rec), (const ubyte *)rec); if (opdb_rec == NULL) { - fatal("libpdb couldn't create record"); + fatal(MYNAME ": libpdb couldn't create record"); } if (pdb_AppendRecord(opdb, opdb_rec)) { - fatal("libpdb couldn't append record"); + fatal(MYNAME ": libpdb couldn't append record"); } } @@ -203,15 +211,15 @@ data_write(void) waypoint *waypointp; if (NULL == (opdb = new_pdb())) { - fatal ("new_pdb failed\n"); + fatal (MYNAME ": new_pdb failed\n"); } strncpy(opdb->name, out_fname, PDB_DBNAMELEN); opdb->name[PDB_DBNAMELEN-1] = 0; opdb->attributes = PDB_ATTR_BACKUP; opdb->ctime = opdb->mtime = time(NULL) + 2082844800U; - opdb->type = 0x43577074; /* CWpt */ - opdb->creator = 0x63475053; /* cGPS */ + opdb->type = MYTYPE; /* CWpt */ + opdb->creator = MYCREATOR; /* cGPS */ opdb->version = 0; /* @@ -222,7 +230,7 @@ data_write(void) htable = malloc(ct * sizeof(*htable)); bh = htable; if (!htable) { - fatal ("Cannot get array for sorting waypoints."); + fatal (MYNAME ":Cannot get array for sorting waypoints."); } QUEUE_FOR_EACH(&waypt_head, elem, tmp) { waypointp = (waypoint *) elem; diff --git a/gpspilot.c b/gpspilot.c new file mode 100644 index 000000000..d79097588 --- /dev/null +++ b/gpspilot.c @@ -0,0 +1,202 @@ +/* + Read and write GPSPilot Tracker files. + + Copyright (C) 2002 Robert Lipe, robertlipe@usa.net + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA + + */ + +#include "defs.h" +#include "coldsync/palm.h" +#include "coldsync/pdb.h" + +#define MYNAME "GPSPilot" +#define MYTYPE 0x706f696e /* poin */ +#define MYCREATOR 0x47704c69 /* GpLi */ + +struct record { + pdb_32 longitude; /* Big endian, long * 3.6e6 */ + pdb_32 latitude; /* similarly */ + pdb_16 elevation; /* meters */ + pdb_16 magvar; /* magnetic variation in degrees, neg = west */ +}; + +static FILE *file_in; +static FILE *file_out; +static const char *out_fname; +struct pdb *opdb; +struct pdb_record *opdb_rec; + +static void +rd_init(const char *fname) +{ + file_in = fopen(fname, "r"); + if (file_in == NULL) { + fatal(MYNAME ": Cannot open %s for reading\n", fname); + } +} + +static void +rd_deinit(void) +{ + fclose(file_in); +} + +static void +wr_init(const char *fname) +{ + file_out = fopen(fname, "w"); + out_fname = fname; + if (file_out == NULL) { + fatal(MYNAME ": Cannot open %s for writing\n", fname); + } +} + +static void +wr_deinit(void) +{ + fclose(file_out); +} + +static void +data_read(void) +{ + struct record *rec; + struct pdb *pdb; + struct pdb_record *pdb_rec; + + if (NULL == (pdb = pdb_Read(fileno(file_in)))) { + fatal(MYNAME ": pdb_Read failed"); + } + + if ((pdb->creator != MYCREATOR) || (pdb->type != MYTYPE)) { + fatal(MYNAME ": Not a gpspilot file."); + } + + for(pdb_rec = pdb->rec_index.rec; pdb_rec; pdb_rec=pdb_rec->next) { + waypoint *wpt_tmp; + char *vdata; + + wpt_tmp = calloc(sizeof(*wpt_tmp),1); + if (wpt_tmp == NULL) { + fatal(MYNAME ": cannot allocate memory\n"); + } + + rec = (struct record *) pdb_rec->data; + wpt_tmp->position.longitude.degrees = pdb_read4(&rec->longitude) / 3.6e6; + wpt_tmp->position.latitude.degrees = pdb_read4(&rec->latitude) / 3.6e6; + wpt_tmp->position.altitude.altitude_meters = pdb_read2(&rec->elevation) / 100.0; + + vdata = pdb_rec->data + sizeof(*rec); + + /* + * This maping is a bit contrived. + * Name is up to 36. ID is up to 9. + * Since 'ID' maps more clearly to "shortname" (and this + * more likely to be resemble a wayoint name in another + * receiver) we use that for shortname and use 'name' as + * our description. + */ + wpt_tmp->description = strdup(vdata); + vdata = vdata + strlen(vdata) + 1; + + vdata = vdata + strlen(vdata) + 1; + wpt_tmp->shortname = strdup(vdata); + + waypt_add(wpt_tmp); + + } + free_pdb(pdb); +} + + +static void +cetus_writewpt(waypoint *wpt) +{ + struct record *rec; + static int ct; + struct tm *tm; + + rec = calloc(sizeof(*rec),1); +#if FIXME + strncpy(rec->ID, wpt->shortname, sizeof(rec->ID)); + rec->ID[sizeof(rec->ID)-1] = 0; + strncpy(rec->name, wpt->description, sizeof(rec->name)); + rec->name[sizeof(rec->name)-1] = 0; + + if (wpt->creation_time) { + tm = gmtime(&wpt->creation_time); + rec->min = tm->tm_min; + rec->hour = tm->tm_hour; + rec->sec = tm->tm_sec; + rec->day = tm->tm_mday; + rec->mon = tm->tm_mon + 1; + rec->year = tm->tm_year - 100; + } else { + rec->min = 0xff; + rec->hour = 0xff; + rec->sec = 0xff; + rec->day = 0xff; + rec->mon = 0xff; + rec->year = 0xff; + } + + pdb_write4(&rec->longitude, wpt->position.longitude.degrees * 10000000.0); + pdb_write4(&rec->latitude, wpt->position.latitude.degrees * 10000000.0); + pdb_write4(&rec->elevation, wpt->position.altitude.altitude_meters * 100.0); +#endif + opdb_rec = new_Record (0, 0, ct++, sizeof(*rec), (const ubyte *)rec); + + if (opdb_rec == NULL) { + fatal(MYNAME ": libpdb couldn't create record"); + } + + if (pdb_AppendRecord(opdb, opdb_rec)) { + fatal(MYNAME ": libpdb couldn't append record"); + } +} + +static void +data_write(void) +{ + int i, ct = waypt_count(); + struct hdr *htable, *bh; + queue *elem, *tmp; + extern queue waypt_head; + waypoint *waypointp; +abort(); + if (NULL == (opdb = new_pdb())) { + fatal (MYNAME ": new_pdb failed\n"); + } + + strncpy(opdb->name, out_fname, PDB_DBNAMELEN); + opdb->name[PDB_DBNAMELEN-1] = 0; + opdb->attributes = PDB_ATTR_BACKUP; + opdb->ctime = opdb->mtime = time(NULL) + 2082844800U; + opdb->type = MYTYPE; /* poin */ + opdb->creator = MYCREATOR; /* GpLi */ + opdb->version = 0; +} + + +ff_vecs_t gpspilot_vecs = { + rd_init, + wr_init, + rd_deinit, + wr_deinit, + data_read, + data_write, +}; -- 2.30.2